﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MIT.SysAdmin
{
    public partial class SysAdminNews : System.Web.UI.Page
    {
        String Req1;
        String Req2;
        String Req3;

        String NewsNo;
        protected void Page_Load(object sender, EventArgs e)
        {
            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            Session["CalLoggingInfo"] = Name;
            Session["CalLoggingPosition"] = Position;
            Session["CalLoggingInfoUsername"] = username;

            ///////////////////Notification Number//////////////////////////////////////////////////
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }
        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Login/SignIn.aspx");
        }

        protected void ButtonAddNews_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spGetNewsNo", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@NewsNo", "3");
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    NewsNo = Convert.ToString(dr["NewNo"]);

                }

            }

            if (NewsNo != "3")
            {
                DateTime now = DateTime.Today;
                String dates = now.ToShortDateString();


                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmd = new SqlCommand("spAddNews", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@NewNo", (RadioButtonList1.SelectedValue));
                    cmd.Parameters.AddWithValue("@LiveDate", dates);
                    cmd.Parameters.AddWithValue("@NewsTitle", TextBoxNewsTitle.Text);
                    cmd.Parameters.AddWithValue("@NewsDetails", TextBoxNewsDetails.Text);



                    con.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect("SysAdminNews.aspx");

                }
            }

            else
            {
                LabelNew.Text = "News Limit Exceeds. Delete News";
            }
            


            
        }
    }
}